package com.bonc.vo;

import com.bonc.cst.CST;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回类
 *
 * @param <T>
 * @author zhaiyu
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ResultVo<T> implements Serializable {
    /**
     * 返回状态码
     */
    private String code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public ResultVo(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultVo(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultVo() {
        System.out.println("提示：feign 接口调用需要默认构造参数,故添加此默认构造器.请不要主动使用");
    }

    /**
     * 成功相关方法
     * @param <T> <T>
     * @return <T>
     */
    public static <T> ResultVo<T> getSuccess() {
        return new ResultVo<>(CST.CODE_SUCCESS, CST.MSG_SYS_SUCCESS);
    }

    public static <T> ResultVo<T> getSuccess(T data) {
        return new ResultVo<>(CST.CODE_SUCCESS, CST.MSG_SYS_SUCCESS, data);
    }

    public static <T> ResultVo<T> getSuccess(String msg, T data) {
        return new ResultVo<>(CST.CODE_SUCCESS, msg, data);
    }

    /**
     * 失败相关方法
     *
     * @param <T> <T>
     * @return <T>
     */
    public static <T> ResultVo<T> getFail() {
        return new ResultVo<>(CST.CODE_EXCEPTION, CST.MSG_SYS_FAIL);
    }

    public static <T> ResultVo<T> getFail(String code, String msg) {
        return new ResultVo<>(code, msg);
    }

    public static ResultVo<String> getFailStr(String msg) {
        return new ResultVo<>(CST.CODE_EXCEPTION, msg);
    }
}
